/**
 * Created by leon on 2017/6/20.
 */
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';

import { DataService } from '../data.service';

@Injectable()
export class XAuthResolver implements Resolve<{ token: string }> {
    constructor (
        private dataService: DataService,
        private router: Router
    ) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<{ token: string }> {
        return this.dataService.getXAuth().then(xAuth => {
            if (xAuth) {
                return { token: xAuth };
            } else {
                this.router.navigate(['/'], this.dataService.navExtras);
                return null;
            }
        });
    }
}